Skip to content

[SR-3463][URLSession] config.httpAdditionalHeaders isn't picked up #927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2017

Conversation

enasser
Copy link
Contributor

@enasser enasser commented Mar 29, 2017

@pushkarnk
Copy link
Member

LGTM
@naithar Can you also a take a look please?

var result: [String] = []
var names = Set<String>()
if let hh = currentRequest?.allHTTPHeaderFields {
if httpHeaders != nil {
let hh = httpHeaders as! [String:String]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to use an actual [AnyHashable : Any] dictionary and turn it's keys and values into strings using String(describing:)?
This would also require var names = Set<AnyHashable>(), but AnyHashable can handle strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@naithar Thank you for reviewing the changes and providing your inputs. I will analyze that and get back to you. Sorry for the delay in response as I was on vacation.

Copy link
Contributor Author

@enasser enasser Apr 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@naithar I agree with you that String(describing:) is the preferred way to convert an instance of any type to a string as per the documentation. I think it makes sense if we need to convert only specific elements in the dictionary. Here, we need to process each element in the dictionary and hence converting the dictionary from any type to a string dictionary makes the processing simpler.

We cannot use .lowercased() and .isEmpty methods with the elements of the [AnyHashable : Any] dictionary. Hence, we need to convert them to string before applying those methods.

In order to process the httpHeaders as any type, we need to make following changes.

       var names = Set<String>()
        if httpHeaders != nil {
           // let hh = httpHeaders as! [String:String]
            httpHeaders?.forEach {
                let name = String(describing:$0.0).lowercased()
                guard !names.contains(name) else { return }
                names.insert(name)
                
                if String(describing:$0.1).isEmpty {
                    result.append(String(describing:$0.0) + ";")
                } else {
                    result.append(String(describing:$0.0) + ": " + String(describing:$0.1))
                }
            }
        }

As the element values are already converting to string, I did not change the names variable.

Please let me know your thoughts whether the existing changes are fine or you have any better way.

Copy link
Contributor

@naithar naithar Apr 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes look ok to me

@pushkarnk
Copy link
Member

Thanks @enasser

@swift-ci test and merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants